<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Servo Settings</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }
        .container {
            text-align: center;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            position: relative;
        }
        .rectangle {
            width: 100px;
            height: 200px;
            border: 2px solid #333;
            position: relative;
            margin: 0 auto;
        }
        .line {
            position: absolute;
            width: 50px;
            height: 2px;
            background-color: #333;
        }
        .input-container {
            position: absolute;
            width: 60px;
            text-align: center;
        }
        .input-container input {
            width: 50px;
            font-size: large;
            text-align: center;
        }
        .buttons {
            margin-top: 20px;
        }
        .buttons button {
            padding: 10px 20px;
            margin: 0 10px;
            font-size: 16px;
            cursor: pointer;
        }
        .input-container label {
            display: block;
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Servo Settings</h1>
        <div class="rectangle"></div>
        <!-- Top Left Line and Input -->
        <div class="line" style="top: -2px; left: -2px; transform: rotate(45deg);"></div>
        <div class="input-container" style="top: -60px; left: -60px;">
            <label for="5-0">Thigh</label>
            <input id="5-0" type="number" min="-90" max="90" step="1" value="0">
            <label for="5-1">Leg</label>
            <input id="5-1" type="number" min="-90" max="90" step="1" value="0">
            <label for="5-2">Foot</label>
            <input id="5-2" type="number" min="-90" max="90" step="1" value="0">
        </div>

        <!-- Top Right Line and Input -->
        <div class="line" style="top: -2px; right: -2px; transform: rotate(-45deg);"></div>
        <div class="input-container" style="top: -60px; right: -60px;">
            <label for="0-0">Thigh</label>
            <input id="0-0" type="number" min="-90" max="90" step="1" value="0">
            <label for="0-1">Leg</label>
            <input id="0-1" type="number" min="-90" max="90" step="1" value="0">
            <label for="0-2">Foot</label>
            <input id="0-2" type="number" min="-90" max="90" step="1" value="0">
        </div>

        <!-- Bottom Left Line and Input -->
        <div class="line" style="bottom: -2px; left: -2px; transform: rotate(-45deg);"></div>
        <div class="input-container" style="bottom: -60px; left: -60px;">
            <label for="3-0">Thigh</label>
            <input id="3-0" type="number" min="-90" max="90" step="1" value="0">
            <label for="3-1">Leg</label>
            <input id="3-1" type="number" min="-90" max="90" step="1" value="0">
            <label for="3-2">Foot</label>
            <input id="3-2" type="number" min="-90" max="90" step="1" value="0">
        </div>

        <!-- Bottom Right Line and Input -->
        <div class="line" style="bottom: -2px; right: -2px; transform: rotate(45deg);"></div>
        <div class="input-container" style="bottom: -60px; right: -60px;">
            <label for="2-0">Thigh</label>
            <input id="2-0" type="number" min="-90" max="90" step="1" value="0">
            <label for="2-1">Leg</label>
            <input id="2-1" type="number" min="-90" max="90" step="1" value="0">
            <label for="2-2">Foot</label>
            <input id="2-2" type="number" min="-90" max="90" step="1" value="0">
        </div>

        <!-- Left Middle Line and Input -->
        <div class="line" style="left: 30px; top: 50%; transform: rotate(90deg); height: 50px; width: 2px;"></div>
        <div class="input-container" style="left: -60px; top: 50%; transform: translateY(-50%);">
            <label for="4-0">Thigh</label>
            <input id="4-0" type="number" min="-90" max="90" step="1" value="0">
            <label for="4-1">Leg</label>
            <input id="4-1" type="number" min="-90" max="90" step="1" value="0">
            <label for="4-2">Foot</label>
            <input id="4-2" type="number" min="-90" max="90" step="1" value="0">
        </div>

        <!-- Right Middle Line and Input -->
        <div class="line" style="right: 30px; top: 50%; transform: rotate(-90deg); height: 50px; width: 2px;"></div>
        <div class="input-container" style="right: -60px; top: 50%; transform: translateY(-50%);">
            <label for="1-0">Thigh</label>
            <input id="1-0" type="number" min="-90" max="90" step="1" value="0">
            <label for="1-1">Leg</label>
            <input id="1-1" type="number" min="-90" max="90" step="1" value="0">
            <label for="1-2">Foot</label>
            <input id="1-2" type="number" min="-90" max="90" step="1" value="0">
        </div>

        <div class="buttons">
            <button onclick="undo()">Undo</button>
            <button onclick="save()">Save</button>
        </div>

        <a href="/webctrl">Back to Control Panel</a>
    </div>

    <script>
        // 初始状态查询
        window.addEventListener('load', function() {
            undo();
        });

        function undo() {
            fetch('/api/remote/get-servo-offset', {
                method: 'GET',
            })
            .then(response => response.json())
            .then(data => {
                displayOffset(data);
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }

        function save() {
            fetch('/api/remote/set-servo-offset', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(saveOffset())
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }

        // 将offset二维数组显示在input
        function displayOffset(offset) {
            for (let i = 0; i < offset.length; i++) {
                for (let j = 0; j < offset[i].length; j++) {
                    document.getElementById(`${i}-${j}`).value = offset[i][j];
                }
            }
        }

        // 将input值保存为offset二维数组
        function saveOffset() {
            var offset = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
            for (let i = 0; i < offset.length; i++) {
                for (let j = 0; j < offset[i].length; j++) {
                    offset[i][j] = Number(document.getElementById(`${i}-${j}`).value);
                }
            }
            return offset;
        }
    </script>
</body>
</html>